#ifndef QCOMMUNICATE_H
#define QCOMMUNICATE_H

#include "communicate_global.h"

#include <QObject>
#include <QTimer>
#include <QDateTime>

class Win_Serial;
class QMission;
class QMissionManager;

enum enSerialStatus
{
    SS_OPEN,
    SS_CLOSED
};

enum enCommuStatus
{
    CS_ONLINE,
    CS_OFFLINE,
    CS_CONNECTING,
    CS_PAUSE
};

enum enTrendStatus
{
    ETS_NONE,
    ETS_WATE_DATA,
    ETS_RECV_DATA,
    ETS_DONE
};

class COMMUNICATE_EXPORT tagSerialConfig
{
public:
    int     m_baud_rate;
    int     m_data_length;
    int     m_stop;
    char    m_parity;
    int     m_serial_number;

    bool operator ==(const tagSerialConfig & config)
    {
        return (m_baud_rate==config.m_baud_rate
                && m_data_length==config.m_data_length
                && m_stop==config.m_stop
                && m_parity==config.m_parity
                && m_serial_number==config.m_serial_number);
    }

    bool operator !=(const tagSerialConfig &config)
    {
        return !(*this==config);
    }
};

class COMMUNICATE_EXPORT QCommunicate : public QObject
{
    Q_OBJECT
public:
    explicit QCommunicate(QObject *parent = 0);
    ~QCommunicate();

    bool    open(const tagSerialConfig & config);
    void    closed();

    tagSerialConfig get_config();

    QMissionManager* mission_manager();

    int     commu_status();
    int     serial_status();

    void    start_trend(int time_out);
    void    stop_trend();

    uchar* get_trend_data();
protected:
    void    send_mission_data(QMission* ms);

    void    run();
protected slots:
    void    timer();
    void    recv_data();

    void    machine_offline();
    void    machine_online();

signals:
    void    offline();
    void    online();

    void    trend_start();
    void    trend_recv(int count);
    void    trend_finish();
    void    trend_time_out();
protected:
    Win_Serial      *m_serial;
    QMissionManager *m_mission_manager;

    tagSerialConfig m_config;

    QTimer          *m_timer;

    QString         m_current_mission_id;

    QDateTime       m_last_time;
    uchar           m_recv_data[8200];
    int             m_length;

    enCommuStatus   m_commu_status;
    enSerialStatus  m_serial_status;
    enTrendStatus   m_trend_status;

    int             m_trend_time_out;
};

#endif // QCOMMUNICATE_H
